<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/xadmin/css/font.css}">
    <link rel="stylesheet" th:href="@{/xadmin/css/xadmin.css}">
    <script th:src="@{/xadmin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/xadmin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-body {
            overflow-x: hidden;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">期末统计</a>
                <a>
                    <cite>考试分数</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <select name="termId" lay-verify="required" class="show-term-container"></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select name="deptId" lay-verify="required" class="show-dept-container"></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="set">设置</button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table" id="test" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <div class="layui-inline" title="批量删除" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>
        <div class="layui-inline" title="添加" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
    </div>
</script>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var load = layer.load(1);
        getTerm(function () {
            layui.form.render('select');
            getTable();
            layer.close(load);
        });
        form.on('submit(set)', function (data) {
            console.log(data.field);
            let termId = data.field['termId'];
            let deptId = data.field['deptId'];
            sessionStorage.setItem('termId', termId);
            sessionStorage.setItem("deptId", deptId);
            table.reload('test', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                url: '/examScore/all?termId=' + termId + '&deptId=' + deptId
            });
            // console.log("重新渲染完成termId " + sessionStorage.getItem('termId'));
            return false;
        });
        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'delete':
                    var data = checkStatus.data;
                    var idArray = [];
                    for (var i in data) {
                        idArray[i] = data[i].id;
                    }
                    if (idArray.length === 0) {
                        layer.alert("未选中", {icon: 5});
                        break;
                    }
                    console.log(idArray.toString());
                    layer.confirm('真的要删除?', function (index) {
                        var load = layer.load(2)
                        $.ajax({
                            url: '/examScore/del',
                            type: "DELETE",
                            data: {ids: idArray, termId: sessionStorage.getItem("termId")},
                            dataType: 'json',
                            success: function (result) {
                                layer.close(load)
                                if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                                    layer.alert(result.msg, {icon: 2});
                                } else {
                                    layer.alert("删除成功", {icon: 1}, function (index1) {
                                        layer.close(index1);
                                        //xadmin.father_reload();
                                        table.reload('test');
                                    });
                                }
                            },
                        });
                    });
                    break;
                case 'add':
                    xadmin.open('添加', '/admin/getPage?name=exam/score-add');
                    break;
            }
        });

        function getTerm(callback) {
            $.ajax({
                url: "/term",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = '';
                    if (result.data && result.data.length > 0) {
                        sessionStorage.setItem("termId", result.data[0].id);
                        let termId = result.data[0].id;
                        console.log(termId);
                        for (var i in result.data) {
                            console.log(result.data[i].id);
                            if (result.data[i].id == termId) {
                                console.log("==");
                                html += '<option value="' + result.data[i].id + '" selected="selected">' + result.data[i].name + '</option>';
                            } else
                                html += '<option value="' + result.data[i].id + '">' + result.data[i].name + '</option>';
                        }
                        $(".show-term-container").html(html);
                    }
                    getDept(callback);
                }
            });
        }

        function getDept(callback) {
            $.ajax({
                url: "/dept",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = '';
                    if (result.data && result.data.length > 0) {
                        sessionStorage.setItem("deptId", result.data[0].id);
                        let deptId = result.data[0].id;
                        console.log(deptId);
                        for (var i in result.data) {
                            // console.log(result.data[i].id);
                            if (result.data[i].id == deptId) {
                                console.log("==");
                                html += '<option value="' + result.data[i].id + '" selected="selected">' + result.data[i].name + '</option>';
                            } else
                                html += '<option value="' + result.data[i].id + '">' + result.data[i].name + '</option>';
                        }
                        $(".show-dept-container").html(html);
                    }
                    if (callback) {
                        callback();
                    }
                }
            });
        }

        function getTable() {
            table.render({
                elem: '#test',
                url: '/examScore/all?termId=' + sessionStorage.getItem('termId') + '&deptId=' + sessionStorage.getItem('deptId'),
                toolbar: '#toolbarDemo',
                cols: [
                    [
                        {type: 'checkbox'},
                        {type: 'numbers', width: 80, sort: true, title: '序号'},
                        {field: 'username', minWidth: 100, sort: true, title: '学号'},
                        {field: 'realName', minWidth: 60, title: '姓名'},
                        {field: 'deptName', minWidth: 60, title: '系部'},
                        {field: 'gradeName', minWidth: 60, title: '年级'},
                        {
                            minWidth: 120,
                            title: '分数',
                            templet: function (d) {
                                if (d.score * 1 < 60) {
                                    return '<div><span style="color: red;font-weight: bold;">' + d.score + '</span></div>';
                                } else {
                                    return '<div><span style="color: black;font-weight: bold;">' + d.score + '</span></div>';
                                }
                            }
                        },
                        {field: 'count', minWidth: 120, title: '不及格科目'},
                        {field: 'remark', minWidth: 120, title: '备注'},
                    ]
                ],
                page: true,
                limits: [10, 20, 50, 100, 500, 1000]
            });
        }
    });
</script>
</html>